from time import sleep
from lxml import etree
from selenium import webdriver
import requests
import pymysql

conn = pymysql.connect(host="localhost", user="root", password="544002", db="shiyu", port=3307, charset="utf8")
cursor = conn.cursor()
url = "https://fanqienovel.com/"
drive = webdriver.Chrome()
drive.maximize_window()
# 网址

drive.get(url)
html = drive.page_source
result = etree.HTML(html)
result_html = result.xpath(
    "//div[@class='muye-bottom-choiceness-main male']/div[@class='muye-bottom-choiceness-list']/a[@class='muye-bottom-choiceness-item']")
for i in result_html:
    title = i.xpath("./div[@class='gender-text book-text']/h3[@class='title']/text()")[0]
    author = i.xpath("./div[@class='gender-text book-text']/h4[@class='author']/text()")[0]
    img_url = i.xpath(".//img[@class='book-cover-img']/@src")[0]
    print(title, author, img_url)
    # 插入数据的SQL语句
    insert_sql = "INSERT INTO male_frequency (title, author, img_url) VALUES (%s, %s, %s)"

    # 执行插入操作
    cursor.execute(insert_sql, (title, author, img_url))

# 提交事务
conn.commit()
# 关闭连接
conn.close()